/**
 * Copyright &copy; 2020 <a href="https://github.com/somowhere/exam-system">exam-system</a> All rights reserved.
 */
package com.shanhesoft.java.modules.exam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shanhesoft.java.common.core.vo.PageModel;
import com.shanhesoft.java.common.data.util.QueryWrapperUtil;
import com.shanhesoft.java.common.persistence.service.impl.DataServiceImpl;
import com.shanhesoft.java.modules.exam.domain.VirtualTraining;
import com.shanhesoft.java.modules.exam.domain.VirtualTrainingDetail;
import com.shanhesoft.java.modules.exam.domain.dto.VirtualTrainingDetailQueryCriteria;
import com.shanhesoft.java.modules.exam.domain.dto.VirtualTrainingDto;
import com.shanhesoft.java.modules.exam.domain.vo.VirtualTrainingDetailVo;
import com.shanhesoft.java.modules.exam.domain.vo.VirtualTrainingVo;
import com.shanhesoft.java.modules.exam.repository.VirtualTrainingRepository;
import com.shanhesoft.java.modules.exam.service.VirtualTrainingDetailService;
import com.shanhesoft.java.modules.exam.service.VirtualTrainingService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 课程实训管理ServiceImpl 课程实训管理
 * @author admin
 * @version 2020-11-17 01:02:25
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class VirtualTrainingServiceImpl extends DataServiceImpl<VirtualTrainingRepository, VirtualTraining, VirtualTrainingDto, String> implements VirtualTrainingService{
	private VirtualTrainingDetailService trainingDetailService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveOrUpdate(VirtualTrainingDto virtualTrainingDto) {
		super.saveOrUpdate(virtualTrainingDto);

		// 先删除，再写入
		VirtualTrainingDetailQueryCriteria trainingDetailQueryCriteria = new VirtualTrainingDetailQueryCriteria();
		trainingDetailQueryCriteria.setTrainId(virtualTrainingDto.getId());
		QueryWrapper wrapper = QueryWrapperUtil.getWrapper(trainingDetailQueryCriteria);
		trainingDetailService.remove(wrapper);

		List<VirtualTrainingDetail> trainingDetailList = new ArrayList<VirtualTrainingDetail>();
		getPageQueList(trainingDetailList, virtualTrainingDto.getTrainingList(), virtualTrainingDto.getId());
		trainingDetailService.saveOrUpdateBatch(trainingDetailList);
	}

	private void getPageQueList(List<VirtualTrainingDetail> trainingDetailList,
			List<VirtualTrainingDetailVo> queList, String trainId) {
		if(null != queList) {
			for(VirtualTrainingDetailVo map : queList) {
				VirtualTrainingDetail detail = new VirtualTrainingDetail();
				detail.setTrainId(trainId);
				detail.setTrainQueId(map.getTrainQueId());

				trainingDetailList.add(detail);
			}
		}
	}

	@Override
	public IPage<VirtualTrainingVo> findPage(PageModel pm, QueryWrapper wrapper) {
		return repository.findTrainingVoPage(pm, wrapper);
	}

	@Override
	public VirtualTrainingVo findVoById(String id) {
		return repository.findTrainingVoById(id);
	}
}
